<?php session_start(); // 启动 session
include '../db.php';
if(isset($_GET['username']) || isset($_GET['user_if'])){
    $user = $_GET['username']; // 用户名
    $user_if = $_GET['user_if']; // 用户操作
    if($user_if == 'del'){ // 删除用户
        // 开启事务
        $conn->begin_transaction(); // 开始事务
        try { // 尝试执行以下代码
            $sql1 = "DELETE FROM user_privilege WHERE User_id = (SELECT id FROM usertable WHERE username = '$user')"; // 删除用户权限
            $conn->query($sql1); // 执行查询
            $sql2 = "DELETE FROM usertable WHERE username = '$user'"; // 删除用户
            $conn->query($sql2); // 执行查询
            $conn->commit(); // 提交事务
            echo "<script>alert('删除成功');window.location.href='user_Type.php';</script>"; // 弹出警告框并跳转到 user_Type.php 页面
        } catch (Exception $e) { // 如果发生异常
            $conn->rollback(); // 回滚事务
            echo "<script>alert('删除失败');window.location.href='user_Type.php';</script>"; // 弹出警告框并跳转到 user_Type.php 页面
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="/styles.css">
</head>
<body>
    <div class="container">
        <div class="card">
            <h1>用户管理系统</h1>
            <p class="info message">欢迎你: <?php echo $_SESSION['username']?> (<?php echo $_SESSION['userType']?>)</p>
        </div>
        
        <div class="card">
            <h2 class="table-title">用户列表</h2>
            <div class="table-container">
                <table>
                    <tr>
                        <th>用户名</th>
                        <th>用户类型</th>
                        <th>操作</th>
                    </tr>
                <?php
                    // 查询所有用户
                    $sql = "SELECT username, memo FROM usertable, user_privilege, privilege
                    WHERE usertable.id = user_privilege.User_id AND user_privilege.Privilege_id = privilege.id";
                    $result = $conn->query($sql);
                    // 显示用户列表
                    if ($result->num_rows > 0) {
                        while($row = $result->fetch_assoc()) {
                            echo "<tr>";
                            echo "<td>{$row["username"]}</td>";
                            echo "<td>{$row["memo"]}</td>";
                            echo "<td class='action-cell'>
                                <div class='action-buttons'>
                                    <a href='user_update.php?username={$row["username"]}' class='action-btn blue'>修改</a>
                                    <a href='user_Type.php?username={$row["username"]}&user_if=del' class='action-btn red'>删除</a>
                                </div>
                            </td>";
                            echo "</tr>";
                        }
                    }
                    else {
                        echo "<tr><td colspan='3'>没有用户</td></tr>";
                    }
                    // 关闭数据库连接
                    $conn->close();
                ?>
                </table>
            </div>
            <div style="margin-top: 20px; text-align: center;">
                <a href="../PHP_index/user_index.php" class="action-link">返回</a>
            </div>
        </div>
    </div>
    <?php include '../footer.php'; ?>
</body>
</html>